package com.concurrent.chapter6.concurrent02;

import java.util.concurrent.CompletionService;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

	public static void main(String[] args) {
		try {
			MyCalableA myCalableA = new MyCalableA();
			MyCalableB myCalableB = new MyCalableB();
			Executor executor = Executors.newSingleThreadExecutor();
			CompletionService<String> completionService = new ExecutorCompletionService<>(executor);
			completionService.submit(myCalableB);
			completionService.submit(myCalableA);
			for (int i = 0; i < 2; i++) {
				System.out.println("----" + completionService.poll());
			}
			TimeUnit.SECONDS.sleep(5);
			System.out.println("A处：" + completionService.poll().get());
			System.out.println("B处：" + completionService.poll().get());
			System.out.println("main end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
